package com.kfm.hotel.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.kfm.hotel.domain.IntakeInfo;
import com.kfm.hotel.domain.Room;
import com.kfm.hotel.domain.SysUser;
import com.kfm.hotel.service.IntakeInfoService;
import com.kfm.hotel.util.Constant;
import com.kfm.hotel.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/checkout")
public class CheckOutController {

    @Autowired
    private IntakeInfoService service;

    @GetMapping
    public ModelAndView index(){
        return new ModelAndView("checkout/index");
    }


    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer limit,
                  IntakeInfo entity) {
        PageInfo<IntakeInfo> pageInfo = service.listCheckOut(entity, page, limit);
        return R.ok(pageInfo);
    }

    @GetMapping("/{id}")
    public ModelAndView checkout(@PathVariable Integer id){
        ModelAndView mv = new ModelAndView("checkout/checkout");
        mv.addObject("id", id);
        return mv;
    }

    @GetMapping("/get/{id}")
    public R getInfo(@PathVariable Integer id){
        IntakeInfo intakeInfo = service.getByIdDeep(id, config -> config.loop(true));
        return R.ok(intakeInfo);
    }

    @PostMapping("/checkout")
    public R checkout(@RequestBody IntakeInfo intakeInfo, @SessionAttribute(Constant.LOGIN_USER)SysUser user){
        service.checkOut(intakeInfo, user.getId());
        return R.ok("退房成功");
    }

}
